package carlyang;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/*
 * 宸ュ叿绫伙紝鍋氬瓧绗︿覆澶勭悊锟�?
 */
public class FacadeChatUtils {
	private static FacadeChatUtils utils;

	private FacadeChatUtils() {
		super();
	}

	public static FacadeChatUtils getInstance() {
		if (utils == null)
			utils = new FacadeChatUtils();
		return utils;
	}

	// byte鏁扮粍杞负鍗佸叚杩涘埗瀛楃锟�?
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0)
			return null;
		for (byte element : src) {
			int v = element & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2)
				stringBuilder.append(0);
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	// 瀛楃涓茶浆鍗佸叚杩涘埗缂栫爜
	public static String toHexString(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	public static byte[] AsciiStringtoHexByteArry(String s) {
		byte[] byteArry = new byte[s.length()];
		int j = 0;
		for (int i = 0; i < s.length(); i++) {
			byte ch = (byte)s.charAt(i);
			byteArry[j++] = (byte) (ch & 0xFF);//high 4 bit
			//FacadeUtils.log("Test:"+(j-1), Byte.toString((byte) (ch & 0xFF)));
		}
		return byteArry;
	}
	
	// 鍗佸叚杩涘埗缂栫爜涓哄瓧绗︿覆
	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++)
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}

		try {
			s = new String(baKeyword, "GBK");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	// byte鏁扮粍杞瓧绗︿覆
	public static String tobyteString(byte[] src) {
		if (src == null || src.length <= 0)
			return null;

		try {
			return new String(src, "GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 灏嗗崄鍏繘鍒跺瓧绗︽暟缁勮浆鎹负瀛楄妭鏁扮粍
	 * 
	 * @param data
	 *            鍗佸叚杩涘埗char[]
	 * @return byte[]
	 * @throws RuntimeException
	 *             濡傛灉婧愬崄鍏繘鍒跺瓧绗︽暟缁勬槸涓�涓鎬殑闀垮害锛屽皢鎶涘嚭杩愯鏃跺紓甯�
	 */
	public static byte[] hexStringtoBytes(char[] data) {
		int len = data.length;
		if ((len & 0x01) != 0) {
			throw new RuntimeException("Odd number of characters.");
		}
		byte[] out = new byte[len >> 1];
		// two characters form the hex value.
		for (int i = 0, j = 0; j < len; i++) {
			int f = toDigit(data[j], j) << 4;
			j++;
			f = f | toDigit(data[j], j);
			j++;
			out[i] = (byte) (f & 0xFF);
		}
		return out;
	}

	/**
	 * 灏嗗崄鍏繘鍒跺瓧绗﹁浆鎹㈡垚涓�涓暣鏁�
	 * 
	 * @param ch
	 *            鍗佸叚杩涘埗char
	 * @param index
	 *            鍗佸叚杩涘埗瀛楃鍦ㄥ瓧绗︽暟缁勪腑鐨勪綅缃�
	 * @return 涓�涓暣鏁�
	 * @throws RuntimeException
	 *             褰揷h涓嶆槸涓�涓悎娉曠殑鍗佸叚杩涘埗瀛楃鏃讹紝鎶涘嚭杩愯鏃跺紓甯�
	 */
	protected static int toDigit(char ch, int index) {
		int digit = Character.digit(ch, 16);
		if (digit == -1) {
			throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
		}
		return digit;
	}

	public static ArrayList<Byte> convertArrayToList(byte[] src) {
		ArrayList<Byte> byteList = new ArrayList<Byte>();
		if (src != null)
			for (int i = 0; i < src.length; i++) {
				byteList.add(src[i]);
			}
		return byteList;
	}

	public static byte[] convertListToArray(ArrayList<Byte> src) {
		if (src != null) {
			byte[] bytes = new byte[src.size()];
			for (int i = 0; i < src.size(); i++) {
				bytes[i] = src.get(i);
			}
		return bytes;
		}
		return null;
	}

    public static String bytesToString(byte[] src) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < src.length; i++) {
            sb.append((char)src[i]);
        }
        return sb.toString();
    }

    public static String DateToString(Date date,String formatter){
        if(date == null)
            date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

    public static Date StringToDate(String timeStr,String formatter){
        String nowtime = new String(timeStr);
        SimpleDateFormat sdf = new SimpleDateFormat(formatter.trim());
        try {
            Date date = sdf.parse(nowtime);
            return date;
        } catch (ParseException e) {
            FacadeUtils.log("FacadeProtocolData", "StringToDate error");
        }
        return null;
    }

	//convert 4bytes array to Int.
	public static int byte4ArrayToInt(byte[] b) {
//		byte[] a = new byte[4];
//		int i = a.length -1, j = b.length -1;
//		for(; i>=0; i--,j--) {
//			if(j>=0)
//				a[i] = b[j];
//			else
//				a[i] = 0;//if b.length <4, fill high bits to 0.
//		}

		int v0 = (b[0]&0xff) << 24; //&0xff灏哹yte鍊兼棤宸紓杞垚int銆傞伩鍏峧ava鑷姩绫诲瀷鎻愬崌锛屼細淇濈暀楂樹綅鐨勭鍙蜂负
		int v1 = (b[1]&0xff) << 16;
		int v2 = (b[2]&0xff) << 8;
		int v3 = (b[3]&0xff);

		return v0 + v1 + v2 + v3;
	}
	//鎶奿nt鏁版嵁绫诲瀷杞崲鎴�4涓猙yte鏁扮粍
	public static byte[] IntTo4ByteArray(int b) {
		byte[] array = new byte[4];
		array[0] = (byte) ((b & 0xff000000)>>24);
		array[1] = (byte) ((b & 0x00ff0000)>>16);
		array[2] = (byte) ((b & 0x0000ff00)>>8);
		array[3] = (byte) (	b & 0x000000ff);

		return array;
	}

	//姣旇緝涓や釜byte绫诲瀷鏁扮粍鏄惁鏁版嵁涓�鑷�
	public static boolean ByteArrayCompare(byte[] a, byte[] b,int len) {
		for(int i=0;i<len;i++){
			if(a[i] != b[i]) return false;
		}
		return true;
	}
}
